package org.sikyon.chocolatebiscuit.model.entities;

abstract class SelfContainedIdentity<K> extends Entity<K> {

    protected abstract String idProperty();

    @Override
    public K id() {
        try {
            @SuppressWarnings("unchecked")
            K id = (K) this.getClass().getMethod(idProperty()).invoke(this);
            return id;
        } catch (Exception e) {
            // TODO throw a RuntimeException or an UnidentifiableEntityException, which extends RuntimeException
            // the unidentifiable entity exception should indicate the name of the method that couldn't be invoked
        }
        return null;
    }

    public void id(K id) {
        // TODO add a meaningful message
        // since the id property is self contained & the entities are immutable, the id cannot be changed.
        throw new UnsupportedOperationException();
    }
}
